package com.bytedance.improvingroad.widget

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.View
import android.widget.LinearLayout
import androidx.core.view.NestedScrollingParent
import androidx.core.view.NestedScrollingParentHelper
import androidx.core.view.children

/**
 * Created by chenxiong
 * date 1/23/22
 */
class DefineNestedScrollParent @JvmOverloads constructor(
    context: Context,
    attr: AttributeSet? = null,
    style: Int = 0
) : LinearLayout(context, attr, style), NestedScrollingParent {

    private lateinit var helper: NestedScrollingParentHelper

    init {
        init()
    }
    private fun init() {
        helper = NestedScrollingParentHelper(this)
    }

    private var realHeight = 0

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        children.forEach {
            val heightSpec = MeasureSpec.makeMeasureSpec(
                MeasureSpec.getSize(heightMeasureSpec),
                MeasureSpec.UNSPECIFIED
            )
            measureChild(it, widthMeasureSpec, heightSpec)
            realHeight += it.measuredHeight
        }
        setMeasuredDimension(
            MeasureSpec.getSize(widthMeasureSpec),
            MeasureSpec.getSize(heightMeasureSpec)
        )
    }

    private fun showImg(dy: Int) : Boolean{
        val view = getChildAt(0)
        return dy < 0 && scrollY > 0 && !view.canScrollVertically(-1)
    }

    private fun hideImg(dy: Int) : Boolean {
        val view = getChildAt(0)
        return dy > 0 && scrollY < view.height
    }

    override fun scrollTo(x: Int, y: Int) {
        val view = getChildAt(0)
        var tempY = y
        if (y < 0 ) {
            tempY = 0
        }
        if (y > view.height) {
            tempY = view.height
        }
        if (y != scrollY) {
            super.scrollTo(x, y)
        }
    }

    override fun onStartNestedScroll(child: View, target: View, axes: Int): Boolean {
        return true
    }

    override fun onNestedScrollAccepted(child: View, target: View, axes: Int) {
        helper.onNestedScrollAccepted(child, target, axes)
    }

    override fun onStopNestedScroll(target: View) {
        helper.onStopNestedScroll(target)
    }

    override fun onNestedScroll(
        target: View,
        dxConsumed: Int,
        dyConsumed: Int,
        dxUnconsumed: Int,
        dyUnconsumed: Int
    ) {

    }

    override fun onNestedPreScroll(target: View, dx: Int, dy: Int, consumed: IntArray) {
        val show = showImg(dy)
        val hide = hideImg(dy)
        //        Log.i("onMeasure","show: " + show + ", hide: " + hide);
        if (show || hide) {
            consumed[1] = dy
            scrollBy(0, dy)
        }

    }

    override fun onNestedFling(
        target: View,
        velocityX: Float,
        velocityY: Float,
        consumed: Boolean
    ): Boolean {
        return true
    }

    override fun onNestedPreFling(target: View, velocityX: Float, velocityY: Float): Boolean {
        return true
    }

}